import 'package:citiesocial/_include.dart';

///
///单个商品的组件，方形的，默认只需要传Goods这个bean类，同时可以传图片的宽、高（默认200）、整体的宽、对外的margin
///
class SquareGoodsItem extends StatelessWidget {
  final GoodsEntity goods;
  final double imageHeight, imageWidth, width;
  final EdgeInsetsGeometry margin;

  SquareGoodsItem(
    this.goods, {
    this.imageHeight: 200.0,
    this.imageWidth,
    this.width,
    this.margin: const EdgeInsets.all(0.0),
  }) : super();

  /// 生成一个图片
  Image _generateImage() {
    return imageWidth != null && imageWidth > 0
        ? Image(
            image: CachedNetworkImageProvider(
              goods.goods_img,
            ),
            height: imageHeight,
            width: imageWidth,
          )
        : Image(
            image: CachedNetworkImageProvider(
              goods.goods_img,
            ),
            height: imageHeight,
          );
  }

  @override
  Widget build(BuildContext context) {
    final Image image = _generateImage();
    return GestureDetector(
      child: Container(
        margin: margin,
        width: width,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            image,
            Container(
              width: image.width,
              child: Text(
                goods.goods_name ?? '',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 16.0,
                ),
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
              ),
            ),
            Center(
              child: Text(
                '${goods.price ?? goods.goods_price ?? goods.shop_price ?? goods.market_price ?? goods.origin_price}',
                style: TextStyle(
                  color: Colors.grey,
                  fontSize: 13.0,
                ),
                maxLines: 1,
              ),
            ),
          ],
        ),
      ),
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
              builder: (BuildContext context) => GoodsDetail(
                    goods,
                  )),
        );
      },
    );
  }
}
